function GamaHat = PPgrad(Gn, d, ThetaHat, n, delta);
% PP_GRAD.M         Version I.K. - 10/04/07
% Computes a consistent estimate of the derivative of Gn evaluated at
% ThetaHat. We use the estimator GamaHat suggested by Pakes&Pollard (Econometrica 1989)
% given by:
%       GamaHat[n] = eps[n]^(-1)*[Gn(ThetaHat + eps[n]) - Gn(ThataHat)]
% where
%       eps[n] = n^(-delta) leads to a consistent estimator if delta <= 1/2.
%
% NB: this estimator is CONSISTENT even if Gn is non-differentiable!!!
% 
% Inputs:   Gn = pointer to a function to be "differentiated";
%           d = dimension of the vector Gn;
%           ThetaHat = point on which we evaluate GamaHat;
%           n = sample size;
%           delta = constant such that delta <= 1/2 (cf. consistency);
% Output:   GamaHat = Pakes&Pollard estimator of the derivative of Gn;

global INSTRUMENTS

d1 = size(ThetaHat,1);       % dimension of our parameter vector
d2 = d * size(INSTRUMENTS,2);
GamaHat = zeros(d1,d2);
epsn = n^(-1*delta);        % sequence that CV to zero
for i = 1:1:d1
    ui = zeros(d1,1);
    ui(i) = 1;
    Gn1 = feval(Gn,ThetaHat + epsn.*ui);
    Gn2 = feval(Gn,ThetaHat - epsn.*ui);
    GamaHat(i,:) = (Gn1 - Gn2)';
    GamaHat(i,:) = GamaHat(i,:)./(2*epsn);
end